"use strict";

const $arrOrganList = (list) => {
    let steps = [];

    const __isChild = (item, target) => {
        if (!item.children) return false;
        for (let i = 0; i < item.children.length; i++) {
            if (item.children[i].id === target.id) {
                return true;
            }
        }
        return false;
    }

    const __getChildren = (item, list) =>{
        let i = 0;
        for(let i = 0; i < list.length; i++) {
            if (!__isChild(item, list[i]) && list[i].parentIds.indexOf(item.id) >= 0) {
                let child = list[i];
                item.children = item.children || [];
                item.children.push(child);
            }
        }
        if (item.children && item.children.length) {
            for(i = 0; i < item.children.length; i++) {
                __getChildren(item.children[i], list);
            }
        }
    }
    for(let i = 0; i < list.length; i++) {
        if (!list[i].parentIds || !list[i].parentIds.length) {
            steps.push(list[i]);
            __getChildren(list[i], list);
        }
    }
    return steps
}

export default {
    arrOrganList: $arrOrganList
};
